package com.example.dubbo.storage.dubbo;

import com.alibaba.fastjson.JSON;
import com.example.dubbo.api.dto.CommodityDTO;
import com.example.dubbo.api.response.ObjectResponse;
import com.example.dubbo.api.dubbo.StorageService;
import com.example.dubbo.storage.service.StorageTblService;
import io.seata.core.context.RootContext;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Please add the follow VM arguments:
 * <pre>
 *     -Djava.net.preferIPv4Stack=true
 * </pre>
 * @author Liyuhang
 */
@DubboService(version = "1.0.0",protocol = "${dubbo.protocol.id}",
        application = "${dubbo.application.id}",registry = "${dubbo.registry.id}",
        timeout = 3000)
public class StorageServiceImpl implements StorageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StorageService.class);

    @Autowired
    private StorageTblService storageTblService;


    @Override
    public ObjectResponse deduct(CommodityDTO commodityDTO ) {
        LOGGER.info("Storage Service Begin ... xid: " + RootContext.getXID());
        LOGGER.info("Deducting inventory SQL: update storage_tbl set count = count - {} where commodity_code = {}",
                commodityDTO.getCount(), commodityDTO.getCommodityCode());

        ObjectResponse response = storageTblService.decreaseStorage(commodityDTO);
        LOGGER.info("Storage Service End ... :{}",JSON.toJSONString(response));
        return response;
    }

}
